/*
 * Created by SharpDevelop.
 * User: David
 * Date: 2006-12-20
 * Time: 04:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Diagnostics;

using Labb4.Pooman.Game;
using Labb4.Pooman.Cards;
using Labb4.Rules;

namespace Labb4.Pooman.Players
{
	/// <summary>
	/// An ai for playing phase 2, with playing cards that are concecutive in suit as agenda
	/// Also it plays lower cards first
	/// It also saves the trump cards, and are only allowed to play these in suit if the ai
	/// decides it can finish the game in the next two moves, or if it only has trump on hand
	/// </summary>
	public class AiGameStatePhase2 : AiGameState
	{
		#region constructors
		
		public AiGameStatePhase2() :
			base()
		{
		}
		
		#endregion
		
		#region overrides
		
		/// <summary>
		/// This method chooses the cards the AI wants to play.
		/// 
		/// This perticular AI selects cards based on how fast it can win.
		/// </summary>
		/// <param name="player">A reference to the player making the move</param>
		public override void SelectCardsAccordingToPhase(Player player)
		{
			//1. what suit are we playing?
			//		check out the last play in the round
			//2. what's the lowest card we can play in that suit?
			//		check out the last card in the last play in the round
			//3. are we forced to play trumph?
			//		in that case, don't play suits, unless we have only one more play to finish after this one
			
			//get the highest card on the table, the card I have to beat
			Card highestCard = PoomanRules.Instance.CurrentRound.GetHighestCardInPlay(PoomanRules.Instance.CurrentTrump);
			
			//now, find out if I'm alloud to play stragihts (in case I play trumph)
			bool playStraights = true;
			
			//am I two plays from a win?
			int breaks = 0;
			bool firstCardFound = true;
			int lastValue = 0;
			Card.Suit lastSuit = (Card.Suit)0;
			
			foreach(Card it in player.CurrentHand.Cards)
			{
				if(!firstCardFound)
				{
					if(it.SuitOfCard != lastSuit)
					{
						breaks++;
					}
					else
					{
						if((lastValue + 1) != it.ValueOfCard)
						{
							breaks++;
						}
					}
				}
				else
				{
					firstCardFound = false;
				}
				
				lastValue = it.ValueOfCard;
				lastSuit = it.SuitOfCard;
			}
			
			//if I'm more than 2 plays from winning, I'm not alloud to play straights on trumph, must save those
			if(breaks > 1)
			{
				playStraights = false;
			}
			
			//alright, now find the first playable card
			Card firstCard = null;
			
			if(highestCard != null)
			{
				foreach(Card it in player.CurrentHand.Cards)
				{
					if(firstCard == null)
					{
						if(it.SuitOfCard == highestCard.SuitOfCard)
						{
							if(it.ValueOfCard > highestCard.ValueOfCard)
							{
								firstCard = it;
								firstCard.SelectCard(true);
							}
						}
					}
				}
			}
			else
			{
				//we're first in line, and can play whatever we want.
				//let's play the lowest card we've got, without playing the trumph!
				foreach(Card it in player.CurrentHand.Cards)
				{
					if(it.SuitOfCard != PoomanRules.Instance.CurrentTrump.TrumpSuit)
					{
						if(firstCard == null)
						{
							firstCard = it;
						}
						else
						{
							if(it.ValueOfCard < firstCard.ValueOfCard)
							{
								firstCard = it;
							}
						}
					}
				}
				
				if(firstCard == null)
				{
					//oh, what do you know, I only have trumph left.. let's play the first one
					//and let's make sure I can play in straights too!
					playStraights = true;
					firstCard = player.CurrentHand.Cards[0];
				}
			}
			
			//alright, if I don't have a card in the suit I'm supposed to play, that's higher
			//than the one on the table, then I have to think again..
			//if the card on the table is a trumph, then I'm screwed, but if it's not, then I
			//can always play the trumph.
			if(firstCard == null)
			{
				if(highestCard.SuitOfCard != PoomanRules.Instance.CurrentTrump.TrumpSuit)
				{
					foreach(Card it in player.CurrentHand.Cards)
					{
						if(firstCard == null)
						{
							if(it.SuitOfCard == PoomanRules.Instance.CurrentTrump.TrumpSuit)
							{
								firstCard = it;
							}
						}
					}
				}
			}
			
			//did we find a card to play?
			if(firstCard != null)
			{
				firstCard.SelectCard(true);
				//is it a trumph? and are we then supposed to play straights..
				//OR is it maybe NOT a trumph?
				if(((firstCard.SuitOfCard == PoomanRules.Instance.CurrentTrump.TrumpSuit) && playStraights) ||
					(firstCard.SuitOfCard != PoomanRules.Instance.CurrentTrump.TrumpSuit))
				{
					foreach(Card it in player.CurrentHand.Cards)
					{
						if(firstCard.SuitOfCard == it.SuitOfCard)
						{
							if((firstCard.ValueOfCard + 1) == it.ValueOfCard)
							{
								firstCard = it;
								firstCard.SelectCard(true);
							}
						}
					}
				}
			}
			
			//TODO find out if the next player in stick has less than 6 cards on hand
			//in that case, play really high trumph..
			
			
			//if no cards are selected when the play button is pressed, this is enterpreted as
			//meaning that you can't play, and you have to pick up cards..
			
			if(player.CurrentHand.SelectedCards() == 0)
			{
				Debug.WriteLine("Oh no! I'm gonna have to pick up cards no... :(");
				Debug.WriteLine("");
				Debug.WriteLine("");
			}
			
			player.Controler.PressedPlay = true;
		}
		
		#endregion
	}
}
